﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.Framework;
using System.IO;
using CNVP.JShop.UI;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Admin
{
    public partial class AdEdit : AdminPage
    {
        BLL.CNVP_Ads bll = new BLL.CNVP_Ads();
        Model.CNVP_Ads model = new Model.CNVP_Ads();
        public int id = 0;
        public string picOriginal = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 133)) return;//判断页面权限
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]) && Public.IsNumber(Request.QueryString["ID"]))
            {
                id = Convert.ToInt32(Request.QueryString["ID"]);
            }

            if (!IsPostBack)
            {
                txtPostTime.Attributes.Add("ReadOnly", "true");
                txtOpenTime.Attributes.Add("ReadOnly", "true");
                if (id > 0)
                {
                    lbTitle.Text = " > 修改套黄广告";
                    DataInfo(id);
                }
                else
                {
                    lbTitle.Text = " > 添加套黄广告";
                }
                
            }

        }

        public void DataInfo(int id)
        {
            model = bll.GetSingle_CNVP_AdsById(id);
            if (model != null)
            {
                txtServerName.Text = model.ServerName;
                txtLServerName.Text = model.DownLoadLink;
                txtDownLoadUrl.Text = model.DownLoadUrl;
                txtOpenTime.Text = model.OpenTime;
                txtEditionInfor.Text = model.EditionInfor;
                txtHomeLink.Text = model.HomeLink;
                txtHomeLinkUrl.Text = model.HomeLinkUrl;
                txtPostTime.Text = model.AdvanceTime;

                rblIsShow.SelectedValue = model.IsShow.ToString();
                txtOrderID.Text = model.OrderID.ToString();
                ddlDisplayID.SelectedValue = model.DisplayType.ToString();
            }
        }

        protected void btUp_Click(object sender, EventArgs e)
        {
            txtPostTime.Attributes.Add("ReadOnly", "false");
            txtOpenTime.Attributes.Add("ReadOnly", "false");
            DateTime OverdueTime = Convert.ToDateTime(txtPostTime.Text.Trim());
            DateTime nowTime = DateTime.Now;
            TimeSpan npSpan = nowTime - OverdueTime;
            if (npSpan.Seconds >= 1)
            {
                MessageUtils.ShowPre("您选择的时间已过期！");
            }
            if (id > 0)
            {
                #region 修改
                int rows = new BLL.CNVP_Ads().Update_CNVP_Ads(
                       new Model.CNVP_Ads()
                       {
                           ID = id,
                           IsType= 0,
                           ServerName = txtServerName.Text.Trim(),
                           DownLoadLink = txtLServerName.Text.Trim(),
                           DownLoadUrl = txtDownLoadUrl.Text.Trim(),
                           OpenTime = txtOpenTime.Text.Trim(),
                           EditionInfor = txtEditionInfor.Text.Trim(),
                           HomeLink = txtHomeLink.Text.Trim(),
                           HomeLinkUrl = txtHomeLinkUrl.Text.Trim(),
                           AdvanceTime = Public.DateToStamp(Convert.ToDateTime(txtPostTime.Text.Trim())).ToString(),

                           IsShow = byte.Parse(rblIsShow.SelectedValue),
                           OrderID = int.Parse(txtOrderID.Text.Trim() == "" ? "0" : txtOrderID.Text.Trim()),
                           AdClassID = 105,
                           DisplayType = ddlDisplayID.SelectedIndex,   //0为：普通套黄；1为：通宵固顶；2为：全天固定；3通宵广告
                           AdminID = CookieAdminID
                           
                       }

               );
                if (rows == 1)
                {
                    BasePage.MessageBox("修改成功！", "AdvanceAds.aspx?Page=" + Request.QueryString["Page"] + "");
                }
                else
                { BasePage.MessageBoxGoHistory("修改失败！"); }
                #endregion
            }
            else
            {
                #region 添加

                int rows = new BLL.CNVP_Ads().Add_CNVP_Ads(new Model.CNVP_Ads()
                {
                    ID = id,
                    IsType = 0,
                    ServerName = txtServerName.Text.Trim(),
                    DownLoadLink = txtLServerName.Text.Trim(),
                    DownLoadUrl = txtDownLoadUrl.Text.Trim(),
                    OpenTime = txtOpenTime.Text.Trim(),
                    EditionInfor = txtEditionInfor.Text.Trim(),
                    HomeLink = txtHomeLink.Text.Trim(),
                    HomeLinkUrl = txtHomeLinkUrl.Text.Trim(),
                    AdvanceTime = Public.DateToStamp(Convert.ToDateTime(txtPostTime.Text.Trim())).ToString(),


                    IsShow = byte.Parse(rblIsShow.SelectedValue),
                    OrderID = int.Parse(txtOrderID.Text.Trim() == "" ? "0" : txtOrderID.Text.Trim()),
                    AddTime = DateTime.Now,
                    AdClassID = 105,
                    DisplayType = ddlDisplayID.SelectedIndex,
                    AdminID = CookieAdminID
                }, 1);    

                if (rows == 1)
                {
                    BasePage.MessageBox("添加成功！", "AdvanceAds.aspx");
                }
                else
                { BasePage.MessageBoxGoHistory("添加失败！"); }
                #endregion
            }
        }
    }
}